﻿using ConfExpert.Enums;
using ConfExpert.Forms.Base;
using ConfExpert.Forms.Rapor;
using DevTeam;

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConfExpert.Forms.SatinAlma
{
    public partial class OtomatikStokGirisi : XForm
    {
        //private Utility.DataNotifier notifier = null;

        public OtomatikStokGirisi()
        {
            InitializeComponent();
            //RepositoryItem ri = RI(Rp.Vendor);
            //XForm x = new XForm();
            //DataTable dt = AppSession.Lookups.Vendor.Select("VendorId in (72,117)").CopyToDataTable();
            //RepositoryItemGridLookUpEdit riVendor = x.GetCustomGLE(dt, "Name", "VendorId");
            //gleVendor.Properties.Assign(riVendor);

            //notifier = new Utility.DataNotifier();
            //notifier.OnDataChanged += OnDataChanged;
            splitContainerControl1.SizeChanged += splitContainerControl1_SizeChanged;
            LoadRequests();

            //btnSave.Enabled = true;
        }

        //void OnDataChanged()
        //{
        //    try
        //    {
        //        ISynchronizeInvoke checker = (ISynchronizeInvoke)this;

        //        // Check if the event was generated from another thread and needs invoke instead
        //        if (checker.InvokeRequired)
        //        {
        //            DataNotifier.DataChanged tempDelegate = new DataNotifier.DataChanged(OnDataChanged);
        //            checker.BeginInvoke(tempDelegate, null);
        //            return;
        //        }

        //        // If not coming from a seperate thread  we can access the Windows form controls
        //        LoadRequests();
        //    }
        //    catch (Exception ex)
        //    {
        //        LogManager.LogToDB(ex);
        //    }
        //}

        private void LoadRequests()
        {
            DataTable dt = ExecuteDataTable("GetEtiketRequest");
            if (dt.Rows.Count > 0)
            {
                string rollIds = dt.Rows[0][0].ToStr();

                if (rollIds != "")
                {
                    Notify(rollIds);
                    Etiket etiket = new Etiket(EtiketTipi.RollId, rollIds);
                    etiket.PrintingSystem.ShowMarginsWarning = false;
                    etiket.Yazdir(txtPrinter.EditValue.ToStr());
                    gvMaster.Init(dt);
                }
            }

        }

        //private void gleVendor_EditValueChanged(object sender, EventArgs e)
        //{
        //    DataSet ds = Session.DataManager.ExecuteDataSet("GetPreYukleme", (int)gleVendor.EditValue);
        //    if (ds.Tables.Count != 2)
        //        return;

        //    gvMaster.Init(ds.Tables[0]);
        //    gvMaster.AddDetailGrid(gvDetail, "PreYuklemeId");

        //    gvMaster.FormatAsDate("YuklemeTarihi", "EklemeTarihi");
        //    gvMaster.SetCaptions("YuklemeTarihi:YüklemeTarihi", "ArticleCount:Artikel Adedi", "TotalRolls:Top Adedi", "RollsInStock:Gelen Top Adedi", "TotalQty:Toplam Metraj");

        //    gvMaster.MakeCompulsory("YuklemeTarihi");
        //    gvMaster.MakeReadOnly("YuklemeTarihi", "Notlar");
        //    gvDetail.MakeReadOnly();
        //    gvDetail.MakeColumnInvisible("PreYuklemeId");
        //    gvDetail.FormatAsNumber(0, "RollsInStock");

        //    gvMaster.BestFitColumns();
        //    gvDetail.BestFitColumns();
        //}

        void splitContainerControl1_SizeChanged(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = 700;
        }

        private void BeklenenYuklemeler_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            SqlDependency.Stop(Session.DataManager.ConnectionString);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Enabled = false;
            LoadRequests();
            timer.Enabled = true;
        }

        private void OtomatikStokGirisi_Load(object sender, EventArgs e)
        {
            
        }

    }
}